<?php
header("Content-type: text/html; charset=gbk");
//Needed model file
include_once $_SERVER['DOCUMENT_ROOT']."/model/Stage.php";
include_once $_SERVER['DOCUMENT_ROOT']."/model/Undertake.php";
//Needed dao file
include_once $_SERVER['DOCUMENT_ROOT']."/dao/StageDAO.php";
include_once $_SERVER['DOCUMENT_ROOT']."/dao/UndertakeDAO.php";
/**
 * Created by PhpStorm.
 * User: lwh
 * Date: 2017/7/31
 * Time: 10:37
 */

//接收post数据，并强制转换为关联数组
$data = $_POST['data'];
$para = json_decode($data,true);
$result = acceptTask($para);
echo $result;

//简单接任务版本：无等待时间，直接插入数据库当中
function acceptTask($para){
    //输入参数
    $userId = $para["userid"];
    $taskId = $para["taskid"];
    $currentStage = $para["currentstage"];
    $startTime = $para["starttime"];
    $contractTime = $para["contracttime"];

    //获得当前任务正在进行的stageId
    $stageDAO = new StageDAO();
    $stage = $stageDAO->queryStageByTaskAndIndex($taskId, $currentStage);
    $stageId = $stage->getId();

    /*
     * 将请求插入undertake表格当中
     */

    //构造undertake数据
    $undertake = array();
    $undertake["userId"] = $userId;
    $undertake["stageId"] = $stageId;
    $undertake["start_time"] = $startTime;
    $undertake["end_time"] = null;
    $undertake["status"] = Stage::$STAGE_ONGOING;
    $undertake["contract_time"] = $contractTime;

    //插入
    $undertakeDAO = new UndertakeDAO();
    $result = $undertakeDAO->insert($undertake);

    return $result;
}

?>